package com.qf.fmall.order.controller;

import com.qf.fmall.order.entity.Orders;
import com.qf.fmall.order.service.IOrderItemService;
import com.qf.fmall.order.service.IOrdersService;
import com.qf.fmall.order.vo.AddOrderResultVo;
import com.qf.fmall.pay.WxPayUtils;
import com.qf.fmall.utils.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;

/**
 * <p>
 * 订单  前端控制器
 * </p>
 *
 * @author wbw
 * @since 2023-12-01
 */
@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/order")
public class OrdersController {
    @Autowired
    private IOrdersService ordersService;
    @PostMapping("/add")
    public ResultVo add(@RequestBody Orders orders,Integer[] cids) throws ParseException {
        //1.打印参数信息
        log.info("创建订单接口，orders={},cids={}",orders,cids);
        //2.进行参数校验
        //3.调用service对象进行参数处理
       AddOrderResultVo data = ordersService.addOrder(orders,cids);
        return ResultVo.ok(10000,"success",data);
    }
    @GetMapping("/status/{orderId}")
    public ResultVo status (@PathVariable("orderId") String orderId){
        //1.打印参数信息
        log.info("查询订单状态,orderId={}",orderId);
        //2.进行参数校验

        String status = WxPayUtils.queryOrderStatus(orderId);
        System.out.println(status);
        if (status.equals("SUCCESS")) {
            // 订单支付成功
            return ResultVo.ok(10000,"success",2);
        } else {
            // 订单未支付或支付失败
            return ResultVo.notok(-100017,"error",-1);
        }

    }

}
